if not pattern then pattern = {} end

if not pattern.imp then

dofile (ScriptsRootPath .. "lib\\lib.lua")

pattern.imp = {}

function pattern.imp.OnCandle(source, index, data)
	if not source then return end
	if not index then index = 1 end
	
	local after = {
		lib:GetLastCandle(source, index + 2),
		lib:GetLastCandle(source, index + 1),
		lib:GetLastCandle(source, index)
	}
	local candle = lib:GetLastCandle(source, index + 3)
	local before = {
		lib:GetLastCandle(source, index + 4)
	}
	if not (candle and candle.instrument and before[1]) then return end
	
	local body = math.abs(candle.close - candle.open)
	
	if body * 1.5 < candle.high - candle.low then return end
	
	local is_long = candle.close > candle.open
	local center = lib:NormalizePrice((candle.close + candle.open) / 2, candle.instrument, not is_long)
	local price
	if is_long then
		price = (candle.high + candle.open) / 2
	else
		price = (candle.low + candle.open) / 2
	end
	price = lib:NormalizePrice(price, candle.instrument, is_long)
	
	local low = math.min(after[1].open, after[1].close)
	local high = math.max(after[1].open, after[1].close)
	
	for i = 2, #after do
		if low > math.min(after[i].open, after[i].close) then
			low = math.min(after[i].open, after[i].close)
		end
		if high < math.max(after[i].open, after[i].close) then
			high = math.max(after[i].open, after[i].close)
		end
	end
	
	if (candle.high - candle.low) * 0.5 < high - low then return end
	if is_long and center > low or not is_long and center < high then return end
	
	low = math.min(before[1].open, before[1].close)
	high = math.max(before[1].open, before[1].close)
	
	for i = 2, #before do
		if low > math.min(before[i].open, before[i].close) then
			low = math.min(before[i].open, before[i].close)
		end
		if high < math.max(before[i].open, before[i].close) then
			high = math.max(before[i].open, before[i].close)
		end
	end
	
	if body * 0.5 < high - low then return end
	if is_long and price < low or not is_long and price > high then return end
	
	candle.datetime.is_long__ = is_long
	candle.datetime.hint__ = lib:FormatPrice(price, candle.instrument)
			
	candle.datetime.notify__ = string.format("%s IMP %s",
		candle.instrument.code,
		lib:FormatPrice(price, candle.instrument)
	)
	
	return price, candle.datetime
end

end -- of file
